package com.hwua.web.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.hwua.entity.User;
import com.hwua.service.IUserService;
import com.hwua.service.Impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/user")
public class UserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        String userName = request.getParameter("userName");//登录名
        System.out.println(userName);
        String uName = request.getParameter("uName");//用户真实名字
        String email = request.getParameter("email");
        System.out.println(email);
        IUserService userService=new UserServiceImpl();
        HttpSession session = request.getSession(false);
        Map<String,Object> map=new HashMap<>();
        try {
            User user = userService.upPWD(userName, email);
            System.out.println(user);
            if(user!=null){
                session.setAttribute("user",user);
                System.out.println("haha");
                map.put("success",true);
                map.put("info",user.getPwd());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        String jsonStr= JSON.toJSONString(map, SerializerFeature.PrettyFormat);
        response.getWriter().write(jsonStr);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
